//
//  FHComposeView.swift
//  WeiBo
//
//  Created by qfh on 16/7/16.
//  Copyright © 2016年 qiufuhe. All rights reserved.
//

import UIKit

import pop




class FHComposeView: UIView {
    
    //  父控制器
    
    private  var target: UIViewController?
    
    // 背景图片
    private lazy var bgImageView: UIImageView = UIImageView(image: UIImage.getScenShot().applyLightEffect())
    // compost 图片
    private lazy var composeList: [FHCompose] = FHCompose.comoseLists()
    
    // buttion 数组
    
    private lazy var composeButtons: [FHComposeButton] = [FHComposeButton]()
    
    

    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUi()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    func setupUi() {
    
    
        self.frame.size = CGSize(width: ScreenWidth, height: ScreenHeight)
    
        addSubview(bgImageView)
        
        bgImageView.userInteractionEnabled = true
        
        bgImageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(self).offset(UIEdgeInsetsZero)
        }
        
    
        addChildButton()
    
    }
    
    
    
    
    /**
     添加button
     */
    private func addChildButton() {
        
        
        let buttonWidth: CGFloat = 80
        
        let buttonHeight: CGFloat = 110
        
        
        let margin: CGFloat = (ScreenWidth - buttonWidth * 3) / 4
        
  
        
        for (i, value) in composeList.enumerate() {
        
            let button = FHComposeButton()
            
            button.size = CGSize(width: buttonWidth, height: buttonHeight)
            
            let row: CGFloat = CGFloat(i / 3)

            let col: CGFloat = CGFloat(i % 3)
            
            button.x = margin + col * (margin + buttonWidth)
            
            button.y = row * (margin + buttonHeight) + ScreenHeight
            
            button.setTitle(value.title, forState: .Normal)
            
            button.setImage(UIImage(named: value.icon!), forState: .Normal)
            
            button.composeModel = value
            
            button.addTarget(self, action: "clickButton:", forControlEvents: .TouchUpInside)
            
            bgImageView.addSubview(button)
            
            composeButtons.append(button)
       
        }
        
    
    
    }
    
    
    
    
    
    /**
     开启pop 动画
     */
    private func startAinimation(value: UIButton, i: Int, isUp: Bool) {
    
        let animation = POPSpringAnimation(propertyNamed: kPOPViewCenter)
        
        let y = isUp ? value.centerY - 350 : value.centerY + 350
        
        animation.toValue = NSValue(CGPoint: CGPoint(x: value.centerX, y: y))
        
        animation.springSpeed = 10
        
        animation.beginTime = CACurrentMediaTime() + Double(i) * 0.025
        
        value.pop_addAnimation(animation, forKey: nil)

    
    
    }
    
    
    /**
     添加到控制器上
     */
    
    class func show(target: UIViewController) {
        
        let composeView = FHComposeView()
        
        composeView.target = target
        
        // 获取最外层Windows
        let windows = UIApplication.sharedApplication().windows.last!
        
        windows.addSubview(composeView)
        
        // 开启动画
        
        for (i, value) in composeView.composeButtons.enumerate() {
        
            
            composeView.startAinimation(value, i: i, isUp: true)
            
        
        }
        
    }
    
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {

        
        
        for (i, value) in composeButtons.reverse().enumerate() {
            
            
            startAinimation(value, i: i, isUp: false)
            
            
        }

        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (Int64)(0.3 * Double(NSEC_PER_SEC))), dispatch_get_main_queue(), { () -> Void in
            //  结束刷新
            self.removeFromSuperview()
        })
        
        
        
    }
    
    /**
    按钮点击
    
    - parameter btn: <#btn description#>
    */

    @objc func clickButton(btn: FHComposeButton) {
    

        
        UIView.animateWithDuration(0.25, animations: { () -> Void in
            
            
            for value in self.composeButtons {
            
            
                value.alpha = 0.2
                
                if value == btn {
                
                    value.transform = CGAffineTransformMakeScale(2, 2)
                } else {
                
                    value.transform = CGAffineTransformMakeScale(0.2, 0.2)
                }
                
            
            
            }
            
            
            
            }) { (_) -> Void in
                
                
                UIView.animateWithDuration(0.1, animations: { () -> Void in
                    
                    
                    for value in self.composeButtons {
                        
                        value.alpha = 1.0
                        
                        value.transform = CGAffineTransformIdentity
                        
                    }

                    
                    
                    }, completion: { (_) -> Void in
                        
                        let classType = NSClassFromString(btn.composeModel!.className!) as! UIViewController.Type
                        
                        
                        let vc = classType.init()
                        
                        
                        let nav = UINavigationController(rootViewController: vc)

                        
                        self.target?.presentViewController(nav, animated: false, completion: { () -> Void in
                            
                            self.removeFromSuperview()
                            
                            
                        })
                        
                        
                        
                        
                })

                
                
                
        }
        
        
        
    
    
    }
    
    
    

}
